package j02duotai;

import java.util.Random;

class Fa{}
class Animal extends Fa{
    String name;
    public void run(){
        System.out.println("动物开始移动");
    }
    public void eat(){
        System.out.println("动物吃食物");
    }
}
class Cat extends Animal{
    public void eat(){
        System.out.println(name+"吃鱼");
    }
    // 新特性
    String color;
    public void catchMouse(){
        System.out.println("猫抓老鼠");
    }
}
class Dog extends Animal{
    public void eat(){
        System.out.println(name+"吃骨头");
    }

    public void lookDoor(){
        System.out.println("看大门");
    }
}

class Man extends Animal{
    public Man(String name) {
        this.name = name;
    }
//    public void raise(Cat cat){
//        cat.eat();
//    }
//    public void raise(Dog dog){
//        dog.eat();
//    }

    public void raise(Animal animal){
        animal.eat();
    }
}

public class J1zoo {
    public static void main(String[] args) {
        Man luxun = new Man("鲁迅");
        Cat c1 = new Cat();
        luxun.raise(new Cat());
        luxun.raise(new Dog());
        //多态失去自己的新特性
        //向上转换
        Animal a1 = new Cat();
//        System.out.println(a1.color);
//        a1.catchMouse();
        //失去的东西拿回来
        //向下转换，必须强转(数据真身，必须是目标类型)
        Cat c11 = (Cat)a1;
        System.out.println("c11.color = " + c11.color);
        c11.catchMouse();

        System.out.println("=====");
        Animal a5 = new Dog();
//        Cat c5 = (Cat)a5;
//        c5.catchMouse();
        Dog d5 = (Dog)a5;
        System.out.println("=======");
        Animal a100 = null;
        Random r = new Random();
        if(r.nextInt(10)>5){
            a100 = new Cat();
        }else {
            a100 = new Dog();
        }
        System.out.println("a100 = " + a100);
        if(a100 instanceof Dog){
            //true - dog
            Dog dog100 = (Dog)a100;
            dog100.lookDoor();
        }else{
            Cat cat100 = (Cat)a100;
            cat100.catchMouse();
        }
    }
}

class Zoo{
    public Cat getCat(){
        return new Cat();
    }
    public Dog getDog(){
        return new Dog();
    }

    public Animal getAnimla(){
//        return new Cat();
        return new Dog();
    }
}
//返回值多态
class J2Zooreturn{
    public static void main(String[] args) {
        Zoo zoo = new Zoo();
        Animal animal = zoo.getAnimla();
    }
}

